// 音乐控制器 store
import {reqMusicDetail} from '@/api'
const state = {
    // 音乐控制器
    controller:{
        index:0,
        detail:{},
    },
    // 播放列表
    playlist:[]
}

const mutations = {
    // 设置音乐控制器
    CONTROLLER(state,data){
        state.controller = data
    },
    // 追加歌单所有的歌
    APPENDALL(state, data){
        state.playlist = data
    },
    // 播放列表追加一首歌
    APPENDONE(state, data){
        state.playlist.splice(state.controller.index,0,data);
    },
}

const actions = {
    async controller({commit},id){
        let result = await reqMusicDetail(id)
        commit
        if(result.code == 200){
            // console.log(result)
        }
    },

    // 添加整个歌单
    appendAll({commit, state}, data){
        let {controller} = state
        commit
        // 构建对象
        // 找出下标
        data.list.filter((item, index)=>{
            if(item.id == data.id){
                controller.index = index
                controller.detail = data.list[controller.index]
            }
        })
        commit('CONTROLLER',controller)
        commit('APPENDALL', data.list)
    },


    // 清空播放列表
     


    // 往播放列表最后追加
    async appendOne({commit,state}, id){
        let {playlist, controller} = state
        let result = await reqMusicDetail(id)
        if(result.code == 200){
            commit('APPENDONE',result.songs[0])
            controller.detail = playlist[controller.index]
            commit('CONTROLLER', controller)
        }
    },

    // 下一首歌
    nextSong({commit, state}){
        let {playlist, controller} = state
        // 判断是否为最后一首
        let data = controller
        if(controller.index == playlist.length-1){
            // 重置为第一首
            data.index = 0
            data.detail = playlist[data.index]
        }else{
            // 下一首
            data.index += 1
            data.detail = playlist[data.index]
        }
        commit('CONTROLLER', data)
    },

    // 上一首歌
    lastSong({commit, state}){
        let {playlist, controller} = state
        // 判断是否为最后一首
        let data = controller
        if(controller.index == 0){
            // 重置为最后一首
            data.index = playlist.length-1
            data.detail = playlist[data.index]
        }else{
            // 下一首
            data.index -= 1
            data.detail = playlist[data.index]
        }
        commit('CONTROLLER', data)
    }
}

function filterArtists(value)  {
    // 过滤多个歌手名
    let name = value[0].name;
    if (value.length == 1) {
        return name;
    }
    // 长度大于一
    for(let i = 1; i<value.length ; i++){
        name += " / " + value[i].name
    }
    return name;
}

const getters = {
    // 表格数据
    playlistTableData: (state) => {
        // console.log(state.playlistDetail.musicList.songs)
        let musicList = state.playlist.map(item => {
            return {
                id: item.id,
                title: item.name,
                artist: filterArtists(item.ar),
                album: item.al.name,
            }
        })
        return musicList
    }
}

export default {
    state,
    mutations,
    actions,
    getters
}